package com.xiaoxie.common.excel;


import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.Sheet;

public class CustomColumnWidthStyleStrategy implements SheetWriteHandler {

    private final int[] columnWidth;

    public CustomColumnWidthStyleStrategy(int[] columnWidth) {
        this.columnWidth = columnWidth;
    }

    @Override
    public void beforeSheetCreate(SheetWriteHandlerContext context) {
        SheetWriteHandler.super.beforeSheetCreate(context);
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        SheetWriteHandler.super.beforeSheetCreate(writeWorkbookHolder, writeSheetHolder);
    }

    @Override
    public void afterSheetCreate(SheetWriteHandlerContext context) {
        SheetWriteHandler.super.afterSheetCreate(context);
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        SheetWriteHandler.super.afterSheetCreate(writeWorkbookHolder, writeSheetHolder);
        Sheet sheet = writeSheetHolder.getSheet();
        for (int i = 0; i < columnWidth.length; i++) {
            sheet.setColumnWidth(i, columnWidth[i] * 256);
        }
    }
}
